﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameBase;

namespace MilitaryPluginInterface
{
    public class Team : InvisibleGameEntity
    {
        private List<Factory> _factories = new List<Factory>();
        private List<Military> _infantries = new List<Military>();
        private Military _hero;
        private Color _myColor;

        private string _teamName;
        
        private static List<Team> _teams = new List<Team>();

        private Team(string teamName) 
        {
            _teamName = teamName;
        }

        public static void initAll()
        {
            for (int i = 0; i < Constant.maxTeam; i++)
            {
                _teams.Add(new Team(i.ToString()));
                _teams[i].MyColor = Constant.colors[i];
            }
        }

        public static Team getTeamAtIndex(int index)
        {
            return _teams[index];
        }

        public static List<Team> getAllTeams()
        {
            return _teams;
        }

        public static void clearAllTeam()
        {
            _teams.Clear();
        }

        public string TeamName
        {
            get { return _teamName; }
            set { _teamName = value; }
        }

        public List<Factory> Factories
        {
            get { return _factories; }
            set { _factories = value; }
        }

        public List<Military> Infantries
        {
            get { return _infantries; }
            set { _infantries = value; }
        }

        public Military Hero
        {
            get { return _hero; }
            set 
            {
                _hero = value;
                _hero.TeamOwner = this;
            }
        }

        public Color MyColor
        {
            get { return _myColor; }
            set { _myColor = value; }
        }

        public void buildFactory(Factory factory)
        {
            factory.TeamOwner = this;
            _factories.Add(factory);
        }

        public void addInfantry(Military infantry)
        {
            infantry.TeamOwner = this;
            _infantries.Insert(0, infantry);
        }

        public override void update(GameTime gameTime)
        {
            _hero.update(gameTime);
            for (int i = 0; i < _infantries.Count; i++)
                _infantries[i].update(gameTime);

            for (int i = 0; i < _factories.Count; i++)
                _factories[i].update(gameTime);

            // military regen health if near factory
            for (int i = 0; i < _factories.Count; i++)
            {
                for (int j = 0; j < _infantries.Count; j++)
                {
                    float distance = Vector2.Distance(_infantries[j].Position, _factories[i].Center);
                    if (distance <= _factories[i].RangeRegenHealth && !_infantries[j].isDead() && !_infantries[j].isFullHealth())                    
                        _infantries[j].Health.IsRegenning = true;                    
                    else
                        _infantries[j].Health.IsRegenning = false;
                }

                float distancez = Vector2.Distance(Hero.Position, _factories[i].Center);
                if (distancez <= _factories[i].RangeRegenHealth && !Hero.isDead() && !Hero.isFullHealth())                
                    Hero.Health.IsRegenning = true;                
                else                
                    Hero.Health.IsRegenning = false;                
            }
        }

        public bool isAllMilitariesDead()
        {
            for (int i = 0; i < Infantries.Count; i++)
            {
                if (!Infantries[i].isDead())
                    return false;
            }
            if (!Hero.isDead())
                return false;
            return true;
        }

        public bool isLosed()
        {
            foreach (Factory factory in Factories)
                if (!factory.isDestroyed())
                    return false;
            return true;
        }
    }
}
